home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / metamail / contrib / ServiceMail / src / pdinq / pdinq.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-09  |  6.1 KB  |  327 lines

  1. /*Copyright (c)  1993 Enterprise Integration Technologies Corporation
  2.  
  3. Permission to use, copy, modify, distribute, and sell this software and
  4. its documentation for any purpose is hereby granted without fee, provided
  5. that (i) the above copyright notices and this permission notice appear in
  6. all copies of the software and related documentation, and (ii) the name of
  7. Enterprise Integration Technologies Corporation may not be used in any
  8. advertising or publicity relating to the software without the specific,
  9. prior written permission of Enterprise Integration Technologies Corporation.
  10.  
  11. THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  12. EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  13. WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  14.  
  15. IN NO EVENT SHALL ENTERPRISE INTEGRATION TECHNOLOGIES CORPORATION  BE
  16. LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF
  17. ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  18. PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY
  19. THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  20. PERFORMANCE OF THIS SOFTWARE.
  21. */
  22. #include "pdinq.h"
  23. #include "pdmain.h"
  24.  
  25. int main (argc,argv)
  26.   int argc; 
  27.   char **argv;
  28.   {
  29.     char *line;
  30.     int fd,priority;
  31. #ifdef USESYSLOG
  32.     openlog("pdinq",(LOG_PID | LOG_CONS),FACILITY);
  33. #endif    
  34.     strcpy(spooldir,"./spool");
  35.     procopts(argc,argv);
  36.  
  37.     if(chdir(spooldir)<0)
  38.       error(E_CHDIR,spooldir);
  39.       
  40.     fname=makenam(spooldir,SPOOLPREF);
  41.  
  42.     if((fd=open(fname,O_CREAT | O_WRONLY,1<<7))<0)
  43.       error(E_NOOPEN,fname);
  44.  
  45.     while(line=rwl(stdin,fd))
  46.     {
  47.       if(header>0)
  48.         procl(line);
  49.       if(!header)
  50.       {
  51.         priority=getpriority(defpri);
  52.         setname(priority);
  53.         logmsg();
  54.       }
  55.     }
  56.  
  57.     if(fchmod(fd,384)<0)
  58.       error(E_CHMOD,fname);
  59.     if(close(fd)<0)
  60.       error(E_CLOSE,fname);
  61.       
  62.      exit(0);
  63.   }
  64.  
  65. char *rwl(fp,fd)
  66.   FILE *fp;
  67.   int fd;
  68.   {
  69.     static int alloced;
  70.     static char b;
  71.     static char c=0;
  72.     static char *buf;
  73.     int cur;
  74.     
  75.     if(!alloced)
  76.     {
  77.       alloced=DEFALLOC;
  78.       buf=(char *)malloc(sizeof(char)*DEFALLOC);        
  79.       if(!buf)
  80.       {
  81.         error(E_NOMEM,NULL);
  82.         buf=&c;
  83.         return(buf);
  84.       }
  85.     }
  86.     if(b==EOF)
  87.       return(NULL);
  88.  
  89.     if(b=='\n')
  90.     {
  91.       if(header>0)
  92.         header=0;
  93.       buf[0]='\n';
  94.       buf[1]='\0';
  95.       b=0;
  96.       return(buf);
  97.     }
  98.       
  99.     if(b)
  100.     {
  101.       buf[0]=b;
  102.       cur=1;
  103.     }
  104.     else
  105.       cur=0;
  106.  
  107.     while(1)
  108.     {
  109.       while(((b=rwc(fp,fd))!=EOF)&&(b!='\n'))
  110.       {
  111.         if(!addchar(&buf,&alloced,&cur,b))
  112.           goto carry;
  113.       } 
  114.       if(b=='\n')
  115.       {
  116.         /*Check for header field folding as per RFC822*/
  117.         if(header>0)
  118.         {
  119.           if(((b=rwc(fp,fd))==' ')||(b=='\t'))
  120.           { 
  121.             if(addchar(&buf,&alloced,&cur,b))
  122.               continue;
  123.             else
  124.               goto carry;
  125.           }
  126.           buf[cur++]='\n';
  127.           buf[cur++]='\0';
  128.           return(buf);
  129.         }
  130.         buf[cur++]='\n';
  131.         buf[cur++]='\0';
  132.         b=0;
  133.         return(buf);
  134.       }
  135.       buf[cur]='\0';
  136.       return(buf);
  137.     }
  138.     
  139.    carry:
  140.       buf[cur]='\0';
  141.       return(buf);
  142.   }
  143.           
  144. char rwc(fp,fd)
  145.   FILE *fp;
  146.   int fd;
  147.   {
  148.     static char buffer[BUFFERSIZE];
  149.     static int position;
  150.     char foo;
  151.  
  152.     if(position<BUFFERSIZE)
  153.     if((foo=getc(fp))!=EOF)
  154.       buffer[position++]=foo;
  155.     
  156.     if((position==BUFFERSIZE)||(foo==EOF))
  157.     {
  158.       writeb(buffer,fd,position);
  159.       position=0;
  160.     }
  161.  
  162.     return(foo);
  163.   }
  164.     
  165. addchar(sp,sz,pos,c)
  166.   char **sp;
  167.   int *sz,*pos;
  168.   char c;
  169.   {
  170.      char *tp;
  171.  
  172.      if((*pos)<(*sz-3))
  173.      {
  174.        (*sp)[(*pos)++]=c;
  175.        return(1);
  176.      }
  177.      else
  178.      {
  179.        (*sz)*=2;
  180.        if(tp=(char *)realloc(*sp,sizeof(char)*(*sz)))
  181.        {  
  182.           *sp=tp;
  183.           *sp[(*pos)++]=c;
  184.           return(1);
  185.        }
  186.      }
  187.      error(E_NOMEM,NULL);      
  188.      return(0);
  189.   }      
  190.     
  191. writeb(line,fd,btw)
  192.   char *line;
  193.   int fd;
  194.   int btw;
  195.   {
  196.     int bwr;
  197.     char *cur;
  198.  
  199.     cur=line;
  200.  
  201.     while(btw)
  202.     {
  203.      if((bwr=write(fd,line,btw))<0)
  204.         error(E_WRITE,fname);
  205.       cur+=bwr;
  206.       btw-=bwr;
  207.     }
  208.   }      
  209.  
  210. procopts(argc,argv)
  211.   int argc;
  212.   char **argv;
  213.   {
  214.     extern char *optarg;
  215.     extern int optind;
  216.     int ret;
  217.     while((ret=getopt(argc,argv,"d:p:"))!=-1)
  218.     {
  219.       switch(ret)
  220.       {
  221.         case 'd':
  222.           if(strlen(optarg)>=MAXPATHLEN)
  223.             error(E_TOOLONG,optarg);
  224.           else
  225.             strcpy(spooldir,optarg);
  226.           break;
  227.         case 'p':
  228.           defpri=atoi(optarg);
  229.           break;
  230.         case '?':
  231.           break;
  232.         default:
  233.           break;
  234.       }
  235.     }
  236.   }
  237. error(e,c)
  238.   int e;
  239.   char *c;
  240.   {
  241.     string line;
  242.  
  243.     strcpy(line,err[e].msg);
  244.     if(c)
  245.       strcat(line,c);
  246.     if(err[e].status & SYSTEM)
  247.       strcat(line," because %m");
  248. #ifdef USESYSLOG
  249.     syslog(FACILITY | err[e].level,line);
  250. #endif    
  251.     if(err[e].status & FATAL)
  252.     {
  253.       fprintf(stderr,"PD was unable to process your transaction.\n");
  254.       fprintf(stderr,"Error %d\n",e);
  255.       exit(1);
  256.     }
  257.   }      
  258.         
  259. procl(buf)
  260. char *buf;
  261.   {
  262.     static lnum=0;
  263.  
  264.     if(!lnum++)
  265.       sendmaildata(buf);
  266.     else
  267.     {
  268.       if(usefrom(buf))
  269.          line2from(buf);
  270.       if(!strncasecmp("Date",buf,4))
  271.         line2date(buf);
  272.       if(!strncasecmp("Message-Id",buf,10))
  273.         line2msgid(buf);
  274.     }
  275.     stashline(buf); 
  276.   }
  277.  
  278. logmsg()
  279. {
  280.    char line[1024],buf[1024];
  281.  
  282.    time_t foo;
  283.    strftime(buf,1024,"%TZ%D",gmtime(&hdata.date));
  284.    sprintf(line,"%s\t%s\t%s\t%s",
  285.     fname,hdata.msgid,buf,hdata.from);
  286. #ifdef USESYSLOG
  287.    syslog(LOG_INFO,line);
  288. #endif   
  289.    header=-1;   
  290. }
  291.   
  292. getpriority(pri)
  293.   int pri;
  294.   {
  295.     return(pri);
  296.   }
  297.  
  298. char *makenam(dir,pref)
  299.   char *dir,*pref;
  300.   {
  301.     static string name;
  302.     long t;
  303.     int i;
  304.     t=time(NULL);
  305.  
  306.     i=t%65536;
  307.     
  308.     sprintf(name,"%.3s%.4x%.5d",pref,i,getpid());
  309.     return(name);
  310.   }
  311.     
  312. setname(priority)
  313.   int priority;
  314.  
  315.   {
  316.     static string name;
  317.  
  318.     sprintf(name,"%d_%s",priority,fname);
  319.     rename(fname,name);
  320.     fname=name;
  321.   }    
  322.  
  323.     
  324.     
  325.         
  326.   
  327.